package com.huawei.hms.push;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hwid.core.constants.HwAccountConstants;

public class HmsMessageService extends Service {
    private final Messenger a = new Messenger(new a());

    @SuppressLint({"HandlerLeak"})
    class a extends Handler {
        final /* synthetic */ HmsMessageService a;

        private a(HmsMessageService hmsMessageService) {
            this.a = hmsMessageService;
        }

        public void handleMessage(Message message) {
            if (message == null) {
                HMSLog.e("HmsMessageService", "receive message is null");
                return;
            }
            HMSLog.i("HmsMessageService", "handle message start ");
            Bundle data = message.getData();
            if (data != null) {
                Intent intent = new Intent();
                intent.putExtra("message_id", data.getString("message_id"));
                intent.putExtra("message_type", data.getString("message_type"));
                intent.putExtra("message_body", data.getByteArray("message_body"));
                intent.putExtra("device_token", data.getString("device_token"));
                intent.putExtra("inputType", data.getInt("inputType", -1));
                this.a.a(intent);
            }
            super.handleMessage(message);
        }
    }

    public void onMessageReceived(RemoteMessage remoteMessage) {
    }

    public void onDeletedMessages() {
    }

    public void onMessageSent(String str) {
    }

    public void onSendError(String str, Exception exception) {
    }

    public void onNewToken(String str) {
    }

    public IBinder onBind(Intent intent) {
        HMSLog.i("HmsMessageService", "start to bind");
        return this.a.getBinder();
    }

    public int onStartCommand(Intent intent, int i, int i2) {
        HMSLog.i("HmsMessageService", "start to command , startId = " + i2);
        a(intent);
        return 2;
    }

    public void onDestroy() {
        HMSLog.i("HmsMessageService", "start to destroy");
        super.onDestroy();
    }

    private void a(Intent intent) {
        if (intent == null) {
            HMSLog.e("HmsMessageService", "receive message is null");
            return;
        }
        String stringExtra = intent.getStringExtra("message_id");
        String stringExtra2 = intent.getStringExtra("message_type");
        if ("new_token".equals(stringExtra2)) {
            HMSLog.i("HmsMessageService", "onNewToken");
            onNewToken(intent.getStringExtra("device_token"));
        } else if ("received_message".equals(stringExtra2)) {
            HMSLog.i("HmsMessageService", "onMessageReceived, message id:" + stringExtra);
            onMessageReceived(new RemoteMessage(b(intent)));
        } else if ("sent_message".equals(stringExtra2)) {
            HMSLog.i("HmsMessageService", "onMessageSent, message id:" + stringExtra);
            onMessageSent(stringExtra);
        } else if ("send_error".equals(stringExtra2)) {
            int intExtra = intent.getIntExtra(HwAccountConstants.EXTRA_OPLOG_ERROR, com.huawei.hms.push.a.a.ERROR_UNKNOWN.a());
            HMSLog.i("HmsMessageService", "onSendError, message id:" + stringExtra + " error:" + intExtra);
            onSendError(stringExtra, new SendException(intExtra));
        } else if ("server_deleted_message".equals(stringExtra2)) {
            HMSLog.i("HmsMessageService", "delete message, message id:" + stringExtra);
            onDeletedMessages();
        } else {
            HMSLog.e("HmsMessageService", "Receive unknown message: " + stringExtra2);
        }
    }

    private Bundle b(Intent intent) {
        Bundle bundle = new Bundle();
        bundle.putString("message_id", intent.getStringExtra("message_id"));
        bundle.putByteArray("message_body", intent.getByteArrayExtra("message_body"));
        if (1 == intent.getIntExtra("inputType", -1)) {
            bundle.putInt("inputType", 1);
        }
        return bundle;
    }
}
